package com.xy.framework.util;

import java.beans.PropertyEditorSupport;
import java.util.List;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;

public class JsonPropertyEditor extends PropertyEditorSupport {
	private final boolean allowEmpty;
	private Class clazz;

	/**
	 * Create a new JsonPropertyEditor instance, using the given DateFormat for
	 * parsing and rendering.
	 * <p>
	 * The "allowEmpty" parameter states if an empty String should be allowed
	 * for parsing, i.e. get interpreted as null value. Otherwise, an
	 * IllegalArgumentException gets thrown in that case.
	 * 
	 * @param dateFormat
	 *            DateFormat to use for parsing and rendering
	 * @param allowEmpty
	 *            if empty strings should be allowed
	 */
	public JsonPropertyEditor(Class clazz, boolean allowEmpty) {
		this.allowEmpty = allowEmpty;
		this.clazz = clazz;
	}

	/**
	 * Create a new CustomDateEditor instance, using the given DateFormat for
	 * parsing and rendering.
	 * <p>
	 * The "allowEmpty" parameter states if an empty String should be allowed
	 * for parsing, i.e. get interpreted as null value. Otherwise, an
	 * IllegalArgumentException gets thrown in that case.
	 * <p>
	 * The "exactDateLength" parameter states that IllegalArgumentException gets
	 * thrown if the String does not exactly match the length specified. This is
	 * useful because SimpleDateFormat does not enforce strict parsing of the
	 * year part, not even with {@code setLenient(false)}. Without an
	 * "exactDateLength" specified, the "01/01/05" would get parsed to
	 * "01/01/0005". However, even with an "exactDateLength" specified,
	 * prepended zeros in the day or month part may still allow for a shorter
	 * year part, so consider this as just one more assertion that gets you
	 * closer to the intended date format.
	 * 
	 * @param dateFormat
	 *            DateFormat to use for parsing and rendering
	 * @param allowEmpty
	 *            if empty strings should be allowed
	 * @param exactDateLength
	 *            the exact expected length of the date String
	 */
	public JsonPropertyEditor(boolean allowEmpty) {
		this.allowEmpty = allowEmpty;
	}

	/**
	 * Parse the Date from the given text, using the specified DateFormat.
	 */
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (this.allowEmpty && !StringUtils.hasText(text)) {
			// Treat empty String as null value.
			setValue(null);
		} else {
			try {
				if(List.class==clazz){
					setValue(JSON.parseArray(text, clazz));
				}else{
					setValue(JSON.parseObject(text, clazz));
				}
			} catch (Exception ex) {
				throw new IllegalArgumentException("Could not parse " + clazz.getName() + ": " + ex.getMessage(), ex);
			}
		}
	}

	/**
	 * Format the Date as String, using the specified DateFormat.
	 */
	@Override
	public String getAsText() {
		return (getValue() != null ? JSON.toJSONString(getValue()) : "");
	}

}
